﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace YJKFamilyLibManager.ForProject
{
	public class HcTriStateTreeView : TreeView
	{
		public HcTriStateTreeView()
		{
			this._ilStateImages = new ImageList();
			CheckBoxState state = CheckBoxState.UncheckedNormal;
			for (int i = 0; i <= 2; i++)
			{
				Bitmap bitmap = new Bitmap(16, 16);
				Graphics graphics = Graphics.FromImage(bitmap);
				switch (i)
				{
				case 0:
					state = CheckBoxState.UncheckedNormal;
					break;
				case 1:
					state = CheckBoxState.CheckedNormal;
					break;
				case 2:
					state = CheckBoxState.MixedNormal;
					break;
				}
				CheckBoxRenderer.DrawCheckBox(graphics, new System.Drawing.Point(2, 2), state);
				graphics.Save();
				this._ilStateImages.Images.Add(bitmap);
				this._bUseTriState = true;
			}
		}

		[DefaultValue(false)]
		[Description("Sets tree view to display checkboxes or not.")]
		[Category("Appearance")]
		public new bool CheckBoxes
		{
			get
			{
				return this._bCheckBoxesVisible;
			}
			set
			{
				this._bCheckBoxesVisible = value;
				base.CheckBoxes = this._bCheckBoxesVisible;
				this.StateImageList = (this._bCheckBoxesVisible ? this._ilStateImages : null);
			}
		}

		[Browsable(false)]
		public new ImageList StateImageList
		{
			get
			{
				return base.StateImageList;
			}
			set
			{
				base.StateImageList = value;
			}
		}

		[Description("Sets tree view to use tri-state checkboxes or not.")]
		[Category("Appearance")]
		[DefaultValue(true)]
		public bool CheckBoxesTriState
		{
			get
			{
				return this._bUseTriState;
			}
			set
			{
				this._bUseTriState = value;
			}
		}

		public override void Refresh()
		{
			base.Refresh();
			if (!this.CheckBoxes)
			{
				return;
			}
			base.CheckBoxes = false;
			Stack<TreeNode> stack = new Stack<TreeNode>(base.Nodes.Count);
			IEnumerator enumerator = base.Nodes.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					TreeNode item = (TreeNode)obj;
					stack.Push(item);
				}
				goto IL_B4;
			}
			IL_6A:
			TreeNode treeNode = stack.Pop();
			if (treeNode.StateImageIndex == -1)
			{
				treeNode.StateImageIndex = (treeNode.Checked ? 1 : 0);
			}
			for (int i = 0; i < treeNode.Nodes.Count; i++)
			{
				stack.Push(treeNode.Nodes[i]);
			}
			IL_B4:
			if (stack.Count <= 0)
			{
				return;
			}
			goto IL_6A;
		}

		protected override void OnLayout(LayoutEventArgs levent)
		{
			base.OnLayout(levent);
			this.Refresh();
		}

		protected override void OnAfterExpand(TreeViewEventArgs e)
		{
			base.OnAfterExpand(e);
			foreach (object obj in e.Node.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				treeNode.StateImageIndex = (treeNode.Checked ? 1 : 0);
			}
		}

		protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
		{
			base.OnNodeMouseClick(e);
			int num = (base.ImageList == null) ? 0 : 18;
			if (e.X > e.Node.Bounds.Left - num || e.X < e.Node.Bounds.Left - (num + 16))
			{
				return;
			}
			TreeNode treeNode = e.Node;
			treeNode.Checked = !treeNode.Checked;
			Stack<TreeNode> stack = new Stack<TreeNode>(treeNode.Nodes.Count);
			stack.Push(treeNode);
			do
			{
				treeNode = stack.Pop();
				treeNode.Checked = e.Node.Checked;
				for (int i = 0; i < treeNode.Nodes.Count; i++)
				{
					stack.Push(treeNode.Nodes[i]);
				}
			}
			while (stack.Count > 0);
			bool flag = false;
			treeNode = e.Node;
			while (treeNode.Parent != null)
			{
				foreach (object obj in treeNode.Parent.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj;
					flag |= (treeNode2.Checked != treeNode.Checked);
				}
				int num2 = (int)Convert.ToUInt32(treeNode.Checked);
				treeNode.Parent.Checked = (flag || num2 > 0);
				if (flag)
				{
					treeNode.Parent.StateImageIndex = (this.CheckBoxesTriState ? 2 : 1);
				}
				else
				{
					treeNode.Parent.StateImageIndex = num2;
				}
				treeNode = treeNode.Parent;
			}
		}

		private ImageList _ilStateImages;

		private bool _bUseTriState;

		private bool _bCheckBoxesVisible;
	}
}
